package com.base.cn.platform.os.service.order.check;


import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.order.ShopType;
import com.base.cn.platform.os.entity.order.shop.ShopData;
import com.base.cn.platform.os.service.course.cou.buy.CourseBuyRecordService;
import com.base.cn.platform.os.service.course.qa.qaattend.QaAttendService;
import com.base.cn.platform.os.service.exam.record.ExamBuyRecordService;
import com.base.cn.platform.os.service.manage.config.SysConfigBiz;
import com.base.cn.platform.os.service.op.activity.OpActivityBiz;
import com.base.cn.platform.os.service.op.coupon.OpCouponCodeBiz;
import com.base.cn.platform.os.service.op.coupon.OpCouponShopBiz;
import com.base.cn.platform.os.service.user.member.CusUserMemberBuyRecordBiz;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * 检测订单的商品数据工具，验证订单商品数据使用到了观察设计模式
 *
 * @author s.li
 * @date 2019/05/27
 */
public class OrderCheckHandle {
    public static ExamBuyRecordService examBuyRecordService;
    public static CourseBuyRecordService courseBuyRecordService;
    public static OpCouponCodeBiz opCouponCodeBiz;
    public static OpCouponShopBiz opCouponShopBiz;
    public static OpActivityBiz opActivityBiz;
    public static CusUserMemberBuyRecordBiz cusUserMemberBuyRecordBiz;
    public static SysConfigBiz sysConfigBiz;
    public static QaAttendService qaAttendService;

    /**
     * 验证订单数据
     * @param createOrderDataFrom 订单数据
     * @return Map<String,Object>
     */
    public static Map<String,Object> orderCheck(CreateOrderDataFrom createOrderDataFrom){
        Map<String, List<ShopData>> shopDataListMap = createOrderDataFrom.getShopList().stream().collect(Collectors.groupingBy(e->e.getShopType()));
        //获取售卖方式
        TreeMap<String,String> configMap = sysConfigBiz.getSysConfigContext(ConfigName.SALE);
        String selaSwitch = configMap.get("selaSwitch");
        //会员身份
        boolean memberUser = false;
        //包含有图书、课程、试卷
        if(selaSwitch.equals("3") && (ObjectUtils.isNotEmpty(shopDataListMap.get(ShopType.ORDER_SHOP_BOOK))
                || ObjectUtils.isNotEmpty(shopDataListMap.get(ShopType.ORDER_SHOP_COURSE))
                || ObjectUtils.isNotEmpty(shopDataListMap.get(ShopType.ORDER_SHOP_EXAM)))){
            memberUser = cusUserMemberBuyRecordBiz.checkUserUseMemberPrice(createOrderDataFrom.getUserId());
        }
        createOrderDataFrom.setSelaSwitch(selaSwitch);
        createOrderDataFrom.setMemberUser(memberUser);

        //验证商品数据
        ShopCheckObserver shopCheckObserver = new ShopCheckObserver(createOrderDataFrom);
        shopCheckObserver.examBuyRecordService = examBuyRecordService;
        shopCheckObserver.courseBuyRecordService = courseBuyRecordService;
        shopCheckObserver.qaAttendService =qaAttendService;
        //如果是支付订单，则需要验证优惠券和活动数据
        if(createOrderDataFrom.isPay()){
            //验证优惠券
            if(StringUtils.isNotEmpty(createOrderDataFrom.getCode())){
                CouponCodeCheckObserver couponCodeCheckObserver = new CouponCodeCheckObserver(createOrderDataFrom);
                couponCodeCheckObserver.opCouponCodeBiz = opCouponCodeBiz;
                couponCodeCheckObserver.opCouponShopBiz = opCouponShopBiz;
                couponCodeCheckObserver.cusUserMemberBuyRecordBiz = cusUserMemberBuyRecordBiz;
                couponCodeCheckObserver.sysConfigBiz = sysConfigBiz;
            }
            //验证活动
            if(DataUtil.idIsNotNull(createOrderDataFrom.getActivityId())){
                ActivityCheckObserver activityCheckObserver = new ActivityCheckObserver(createOrderDataFrom);
                activityCheckObserver.opActivityBiz =opActivityBiz;
                activityCheckObserver.sysConfigBiz =sysConfigBiz;
                activityCheckObserver.cusUserMemberBuyRecordBiz =cusUserMemberBuyRecordBiz;
            }
        }
        List<ShopData> oldShopDataList = createOrderDataFrom.cloneShopDataList();
        //通知所有的观察者，让所有的观察者进行数据验证
        Map<String,Object> result =createOrderDataFrom.notifyAllObservers(false);
        String errorType = (String)result.get("errorType");
        if(StringUtils.isNotEmpty(errorType)){
            createOrderDataFrom.setShopList(oldShopDataList);
            result =createOrderDataFrom.notifyAllObservers(true);
        }
        //清空观察者数据对象
        createOrderDataFrom.clear();
        return result;
    }
}
